const app = getApp()
import user from '../../utils/user'
import global from '../../utils/global'
import {
    modifyPortrait,
    getUrgentInfo
} from '../../api/app'
import {
    goApp
} from '../../utils/util'
Page({
    data: {
        healthCard: '',
        data: [],
        userPortrait: '',
        userName: '',
        idCardNum: '',
        phone: '',
        onlineBank: '',
        noOnlineBank: '*** **** *** ****',
        isOnlineBank: false,
        bankNum: '',
        cardPositive: '',
        cardBack: '',
        urgentName: '',
        urgentPhone: '',
        urgentAddress: '',
        emergencyContact: '', // 紧急联系人
        emergencyContactPhone: '', // 紧急联系人电话
        emergencyContactAddr: '', // 紧急联系人地址
        bankInfo: {},
        alipayAccount: '',
        showAlipayAccount: ''
    },
    async onLoad() {},
    async onShow() {
        console.log(user.avatarUrl, 'avatarUrlavatarUrl')
        this.setData({
            userPortrait: user.avatarUrl,
            userName: user.userName,
            divisionName: user.userName.substring(user.userName.length - 2, user.userName.length),
            idCardNum: user.cardNo,
            phone: user.phoneNum,
            onlineBank: user.bankCardNoNetwork,
            bankNum: user.bankCardNo ? user.bankCardNo.replace(/\s/g, '').replace(/....(?!$)/g, '$& ') : '',
            cardPositive: user.ownIdCardFront,
            cardBack: user.ownIdCardBack,
            alipayAccount: user.alipayAccount,
            showAlipayAccount: user.alipayAccount ? user.alipayAccount.replace(/(\d{3})\d{4}(\d{3})/, '$1****$2')  : ''
        })
        await this.getUrgentInfo()
    },
    onModifyPortrait(event) {
        if (!user.userId) {
            console.log(222)
            return
        }
        wx.uploadFile({
            url: global.baseUrl + '/api/v1/blue/user/upload',
            filePath: event.detail.avatarUrl,
            name: 'file',
            success: (res) => {
                modifyPortrait({
                    userHeadUrl: JSON.parse(res.data).data.fileUrl
                }).then(() => {
                    this.setData({
                        isAvatarTips: false
                    })
                    app.toast('修改成功')
                    this.setData({
                        userHeadUrl: JSON.parse(res.data).data.fileUrl,
                        userPortrait: JSON.parse(res.data).data.fileUrl
                    })
                    user.avatarUrl = JSON.parse(res.data).data.fileUrl
                })
            },
            fail() {
                console.log('failfail')
            }
        })
    },
    onIsOnlineBank() {
        this.setData({
            isOnlineBank: !this.data.isOnlineBank
        })
    },
    goAlipay() {
        wx.navigateTo({
            url: '/pages/aliPay-info/aliPay-info',
        })
    },
    async getUrgentInfo() {
        const result = await getUrgentInfo()
        console.log(result, 'getUrgentInfo')
        this.setData({
            healthCard: result.healthCard,
            emergencyContact: result.emergencyContact,
            emergencyContactPhone: result.emergencyContactPhone,
            emergencyContactAddr: result.emergencyContactAddr,
            bankInfo: result
        })
    },
    goUrgent() {
        wx.navigateTo({
            url: '/pages/contract-info/contract-info',
        })
    },
    goBankCard() {
        wx.navigateTo({
            url: `/pages/bank-card/bank-card?userName=${this.data.bankInfo.realName}&openBankName=${this.data.bankInfo.openBankName || ''}&openBankBranch=${this.data.bankInfo.openBankBranch || ''}&openBankCity=${this.data.bankInfo.openBankCity || ''}&bankCardNo=${this.data.bankInfo.bankCardNo || ''}&bankCardImg=${this.data.bankInfo.bankCardImg || ''}`,
        })
    },
    // 去查看健康证/完善健康证
    goHealthCertificate() {
        if (!this.data.healthCard) {
            wx.navigateTo({
                url: '/pages/health-certificate-info/health-certificate-info',
            })
        } else {
            wx.navigateTo({
                url: '/pages/health-certificate-detail/health-certificate-detail?healthcard=' + this.data.healthCard,
            })
        }

    },
    // 去完善身份信息
    goIdCard() {
        if (!user.userId) {
            goApp('applet::/pages/login/login')
        }
        if (this.data.cardPositive && this.data.cardBack) {
            wx.navigateTo({
                url: '/pages/card-detail/card-detail',
            })
            return
        }
        wx.navigateTo({
            url: '/pages/card-info/card-info',
        })
    },
    toAlipayNum() {
        wx.navigateTo({
            url: `/pages/alipay-id/alipay-id?alipayAccount=${this.data.alipayAccount}`,
        })
    }
})